package com.aramis.mydytt.ui.user.act

import androidx.lifecycle.MutableLiveData
import com.aramis.mydytt.db.DBManager
import com.aramis.mydytt.db.DownloadedManager
import com.aramis.mydytt.db.FavoriteManager
import com.aramis.mydytt.ext.logE
import com.aramis.mydytt.ui.base.BaseViewModel
import com.aramis.mydytt.ui.bean.MovieItemBean
import com.aramis.mydytt.ui.common.CommonMovieItemAdapter
import io.reactivex.schedulers.Schedulers

/**
 * Description: UserFavoriteViewModel
 * @author lizhidan
 * Created: 2021/6/4
 */
class UserFavoriteViewModel : BaseViewModel() {
    val movieDbLiveData = MutableLiveData<List<MovieItemBean>?>()
    val deleteDataLiveData = MutableLiveData<List<MovieItemBean>?>()

    fun selectFavorite() {
        val subscribe = DBManager.db?.favoriteMovieDao()?.selectAllO()?.subscribeOn(Schedulers.io())?.subscribe({
            movieDbLiveData.postValue(it.map { favoriteMovieBean -> favoriteMovieBean.toMovieItemBean() })
        }, {
            movieDbLiveData.postValue(null)
        })
        if (subscribe == null) {
            movieDbLiveData.postValue(null)
        } else {
            compositeDisposable.add(subscribe)
        }
    }

    fun selectDownloaded() {
        val subscribe = DBManager.db?.downloadedMovieDao()?.selectAllO()?.subscribeOn(Schedulers.io())?.subscribe({
            movieDbLiveData.postValue(it.map { downloadedMovieBean -> downloadedMovieBean.toMovieItemBean() })
        }, {
            movieDbLiveData.postValue(null)
        })
        if (subscribe == null) {
            movieDbLiveData.postValue(null)
        } else {
            compositeDisposable.add(subscribe)
        }
    }

    fun deleteData(style: Int, dataList: List<MovieItemBean>) {
        when (style) {
            UserFavoriteActivity.STYLE_FAVORITE -> deleteFavoriteData(dataList)
            UserFavoriteActivity.STYLE_DOWNLOADED -> deleteDownloadedData(dataList)
            else -> {
                //error
            }
        }
    }

    private fun deleteFavoriteData(dataList: List<MovieItemBean>) {
        FavoriteManager.deleteFavoriteData(dataList).subscribe({
            if (it != null) {
                val newList = DBManager.db?.favoriteMovieDao()?.selectAll()
                if (newList != null) {
                    deleteDataLiveData.postValue(newList.map { favoriteMovieBean -> favoriteMovieBean.toMovieItemBean() })
                }
            }
        }, {
            logE("喜爱删除失败:${it.message}")
        }).let {
            compositeDisposable.add(it)
        }
    }

    private fun deleteDownloadedData(dataList: List<MovieItemBean>) {
        DownloadedManager.deleteDownloadedData(dataList).subscribe({
            if (it != null) {
                val newList = DBManager.db?.downloadedMovieDao()?.selectAll()
                if (newList != null) {
                    deleteDataLiveData.postValue(newList.map { downloadedMovieBean -> downloadedMovieBean.toMovieItemBean() })
                }
            }
        }, {
            logE("已下载删除失败:${it.message}")
        }).let {
            compositeDisposable.add(it)
        }
    }

    fun markAllSelect(allSelect: Boolean, adapter: CommonMovieItemAdapter) {
        adapter.dataList.forEach { it.selected = allSelect }
        adapter.notifyDataSetChanged()
    }
}